<?php 
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: hot.phtml 4684 2010-08-16 08:44:14Z huuphuoc $
 * @since		2.0.0
 */
?>
<?php 
$this->headTitle($this->translator('article_hot_page_title'));

$this->headLink()
	->appendStylesheet($this->APP_STATIC_SERVER . '/js/jquery.ui/themes/base/ui.core.css')
	->appendStylesheet($this->APP_STATIC_SERVER . '/js/jquery.ui/themes/base/ui.resizable.css')
	->appendStylesheet($this->APP_STATIC_SERVER . '/js/jquery.ui/themes/base/ui.dialog.css')
	->appendStylesheet($this->APP_STATIC_SERVER . '/js/jquery.ui/themes/base/ui.theme.css');

$this->headScript()
	->appendFile($this->APP_STATIC_SERVER . '/js/jquery.ui/ui.core.js')
	->appendFile($this->APP_STATIC_SERVER . '/js/jquery.ui/ui.sortable.js')
	->appendFile($this->APP_STATIC_SERVER . '/js/jquery.ui/ui.draggable.js')
	->appendFile($this->APP_STATIC_SERVER . '/js/jquery.ui/ui.resizable.js')
	->appendFile($this->APP_STATIC_SERVER . '/js/jquery.ui/ui.dialog.js');
?>

<?php $this->headStyle()->captureStart(); ?>
.ui-sortable-placeholder { border: #cccccc 1px dashed; background: #FCC; height: 3em; line-height: 3em; }
.ui-sortable { cursor: move; }
<?php $this->headStyle()->captureEnd(); ?>

<div class="grid_12 t_a_ui_breadcrumb">
	<span><a href="<?php echo $this->url(array(), 'news_article_list'); ?>"><?php echo $this->translator('article_list_breadcrumb'); ?></a></span>
	<span><?php echo $this->translator('article_hot_breadcrumb'); ?></span>	
</div>

<div class="grid_12 t_a_bottom">
	<div class="grid_9 alpha">
		<?php if ($this->numArticles == 0) : ?>	
			<?php echo $this->translator('article_list_not_found'); ?>
		<?php else : ?>
		<?php echo $this->translator('article_hot_guide'); ?>
		<?php endif; ?>
	</div>
	
	<div class="grid_3 omega">
		<div class="t_a_right">
			<a href="<?php echo $this->url(array(), 'news_article_add'); ?>" class="t_a_ui_button_link"><span><?php echo $this->translator('article_add_breadcrumb'); ?></span></a>
		</div>
	</div>
</div>

<?php if ($this->numArticles == 0) : ?>
<div class="grid_12 t_a_bottom">
	<?php echo $this->languageSwitcher('flag', str_repeat('&nbsp', 2)); ?>
</div>
<?php else : ?>
<form action="<?php echo $this->url(array(), 'news_article_hot'); ?>" method="post" name="orderingArticle" id="orderingArticle">	
	<div id="hotArticles" class="grid_12 t_a_bottom">
		<div class="t_a_ui_data_list_header">
			<div class="grid_5 alpha">
				<div><?php echo $this->translator('article_main_title'); ?></div>
			</div>
			
			<div class="grid_2"><?php echo $this->translator('article_status'); ?></div>
			
			<div class="grid_2"><?php echo $this->translator('article_num_views'); ?></div>
			
			<div class="grid_2"><?php echo $this->languageSwitcher('flag', str_repeat('&nbsp', 2)); ?></div>
			
			<div class="grid_1 omega"><?php echo $this->translator('article_id'); ?></div>
		</div>
	
		<?php foreach ($this->articles as $article) : ?>
		<div id="articleRow_<?php echo $article->article_id; ?>" class="clearfix t_a_ui_data_list_content">
			<div class="grid_5 alpha">
				<div>
					<b><?php echo $article->title; ?></b><br />
					<?php echo sprintf($this->translator('article_list_posted_by'), $article->created_user_name, date('Y-m-d H:i:s', strtotime($article->created_date))); ?>
					<input class="t_article_id" type="hidden" name="articles_id[]" value="<?php echo $article->article_id; ?>" />
				</div>
				
				<div class="t_a_top t_a_ui_helper_action">
					<?php if ($this->allow('edit')) : ?>
					<a href="<?php echo $this->url(array('article_id' => $article->article_id), 'news_article_edit'); ?>"><?php echo $this->translator('article_list_action_edit'); ?></a> |
					<?php endif; ?>
					
					<?php if ($this->allow('delete')) : ?> 
					<a class="deleteAction" rel="<?php echo $article->article_id; ?>" href="javascript: void(0);"><?php echo $this->translator('article_list_action_delete'); ?></a> |
					<?php endif; ?>
					
					<?php if ($this->allow('activate')) : ?>
					<a class="activateAction" rel="<?php echo $article->article_id; ?>_<?php echo $article->status; ?>" href="javascript: void(0);"><?php if ($article->status == 'active') : ?><?php echo $this->translator('article_list_action_deactivate'); ?><?php else : ?><?php echo $this->translator('article_list_action_activate'); ?><?php endif; ?></a>
					<?php endif; ?>
				</div>
			</div>
			
			<div class="grid_2" id="articleStatus_<?php echo $article->article_id; ?>">
				<?php if ($article->status == 'active') : ?>
					<?php echo $this->translator('article_list_status_activated'); ?>
				<?php elseif ($article->status == 'inactive') : ?>
					<?php echo $this->translator('article_list_status_not_activated'); ?>
				<?php elseif ($article->status == 'draft') : ?>
					<?php echo $this->translator('article_list_status_draft'); ?>
				<?php elseif ($article->status == 'deleted') : ?>
					<?php echo $this->translator('article_list_status_deleted'); ?>
				<?php endif; ?>
			</div>
			
			<div class="grid_2">
				<?php if ($article->num_views) : ?><?php echo $article->num_views; ?><?php else : ?>0<?php endif; ?>
			</div>
			
			<?php 
			/**
			 * Show translated articles if there is
			 * @since 2.0.8
			 */
			?>
			<div class="grid_2">
			<?php if (($translations = $this->translationItems($article)) != null) : ?>
				<?php foreach ($translations as $language => $translation) : ?>
					<?php if ($translation == null && $language != $article->language) : ?>
						<?php if (isset($translations[$this->APP_DEFAULT_LANG])) : ?>
						<a class="t_a_ui_helper_action_add" href="<?php echo $this->url(array('source_id' => $translations[$this->APP_DEFAULT_LANG]->article_id), 'news_article_add_localization'); ?>/<?php echo $language; ?>/">&nbsp;</a>
						<?php else :?>
						<a class="t_a_ui_helper_action_add" href="<?php echo $this->url(array(), 'news_article_add'); ?>/<?php echo $language; ?>/">&nbsp;</a>
						<?php endif; ?>
					
					<?php elseif (($translation == null && $language == $article->language) || ($translation != null && $translation->article_id == $article->article_id)) : ?>
					<a class="t_a_ui_helper_action_edit" href="<?php echo $this->url(array('article_id' => $article->article_id), 'news_article_edit'); ?>/<?php echo $language; ?>/">&nbsp;</a>
					
					<?php elseif ($translation != null && $translation->article_id != $article->article_id) : ?>
					<a class="t_a_ui_helper_action_edit" href="<?php echo $this->url(array('article_id' => $translation->article_id), 'news_article_edit'); ?>/<?php echo $language; ?>/">&nbsp;</a>
					<?php endif; ?>
				<?php endforeach; ?>
			<?php else : ?>
			&nbsp;
			<?php endif; ?>
			</div>
			
			<div class="grid_1 omega"><?php echo $article->article_id; ?></div>
		</div>
		<?php endforeach; ?>
	</div>
	
	<div class="clearfix grid_12 t_a_bottom">
		<button type="button" id="orderingButton"><span><?php echo $this->translator('article_hot_ordering_button'); ?></span></button>
	</div>
</form>
<?php endif; ?>

<div id="messageDialog"></div>

<div id="confirmDialog"></div>

<script type="text/javascript">
$(document).ready(function() {
	$('#hotArticles').sortable({
		opacity: 0.8,
		revert: true
	}).disableSelection();
	
	$('#orderingButton').click(function() {
		var orders = $('#hotArticles').sortable('serialize');
		
		$.ajaxq('ordering_article', {
			type: 'POST',
			url: "<?php echo $this->url(array(), 'news_article_hot'); ?>",
			data: orders,
			success: function(response) {
				var message = '';
				if (response == 'RESULT_OK') {
					message = '<?php echo addslashes($this->translator('article_hot_ordering_successful')); ?>';
				} else {
					message = '<?php echo addslashes($this->translator('article_hot_ordering_unsuccessful')); ?>';				
				}
				$('#messageDialog').html(message)
					.dialog({
						title: '<?php echo addslashes($this->translator('common_dialog_message')); ?>',
						dialogClass: 't_a_ui_dialog',
						modal: true,
				    	show: { effect: 'fade' },
						open: function(event, ui) {
							setTimeout(function() {
								$('#' + event.target.id).dialog('option', 'hide', 'fade').dialog('destroy');
							}, 3000);
						}
					});
			}
		});
	});
	
	<?php if ($this->allow('delete')) : ?>
	$('a.deleteAction').click(function() {
		var self = this;
		$('#confirmDialog').html('<?php echo addslashes($this->translator('article_list_action_delete_confirm')); ?>')
			.dialog({
				title: '<?php echo addslashes($this->translator('common_dialog_confirm')); ?>',
				dialogClass: 't_a_ui_dialog',
				modal: true,
				closeOnEscape: false,
				resizable: false,
				open: function(event, ui) {
					$('.ui-dialog-titlebar-close').hide();
					$(this).parent().find('.ui-dialog-buttonpane button').wrapInner('<span/>');
				},
				buttons: {
					'<?php echo addslashes($this->translator('common_dialog_confirm_no')); ?>': function() {
						$(this).dialog('destroy');
					},
					'<?php echo addslashes($this->translator('common_dialog_confirm_yes')); ?>': function() {
						$(this).dialog('destroy');
						$(self).html('').addClass('t_a_ui_helper_loading');
						var id = $(self).attr('rel');
						$.ajaxq('news_article', {
							type: 'POST',
							url: "<?php echo $this->url(array(), 'news_article_delete'); ?>",
							data: { id: id },
							success: function(response) {
								if ('RESULT_OK' == response) {
									$('#articleRow_' + id).fadeOut('slow');
								}
							}
						});
					}
				}
			});
	});
	<?php endif; ?>
	
	<?php if ($this->allow('activate')) : ?>
	$('a.activateAction').click(function() {
		var self = this;
		var arr = $(this).attr('rel').split('_');
		$(self).html('').addClass('t_a_ui_helper_loading');
		$.ajaxq('news_article', {
			url: "<?php echo $this->url(array(), 'news_article_activate'); ?>",
			type: 'POST',
			data: { id: arr[0], status: arr[1] },
			success: function(response) {
				$(self).attr('rel', arr[0] + '_' + response);
				if (response == 'active') {
					$('#articleStatus_' + arr[0]).html("<?php echo $this->translator('article_list_status_activated'); ?>");
					$(self).html("<?php echo $this->translator('article_list_action_deactivate'); ?>");
				} else {
					$('#articleStatus_' + arr[0]).html("<?php echo $this->translator('article_list_status_not_activated'); ?>");
					$(self).html("<?php echo $this->translator('article_list_action_activate'); ?>");
				}
				$(self).removeClass('t_a_ui_helper_loading');

				$('#messageDialog').html((response == 'active') ? '<?php echo addslashes($this->translator('article_hot_activated_success')); ?>' : '<?php echo addslashes($this->translator('article_hot_deactivated_success')); ?>')
					.dialog({
						title: '<?php echo addslashes($this->translator('common_dialog_message')); ?>',
						dialogClass: 't_a_ui_dialog',
						modal: true,
				    	show: { effect: 'fade' },
						open: function(event, ui) {
							setTimeout(function() {
								$('#' + event.target.id).dialog('option', 'hide', 'fade').dialog('destroy');
							}, 3000);
						}
					});
			}
		});
	});
	<?php endif; ?>
});
</script>
